package com.zyjc.city.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.zyjc.city.entity.AreaPerson;
import com.zyjc.city.httpclient.MyClient;
import com.zyjc.city.model.ApiResult;
import com.zyjc.city.model.Result;
import com.zyjc.city.model.dto.AreaPersonDto;
import com.zyjc.city.model.dto.TokenDto;
import com.zyjc.city.service.AreaPersonService;
import com.zyjc.city.util.BeanTransUtil;
import com.zyjc.city.util.CaffeineUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @author ckj
 * @date 2024年03月29日 11:04
 */
@RestController
public class DemoController {
    @Autowired
    private MyClient myClient;
    @Autowired
    private AreaPersonService areaPersonService;


    @GetMapping("/test")
    public Result<?> test() {
        TokenDto dto = new TokenDto();
        String value1 = String.valueOf(CaffeineUtil.get("token"));
        dto.setTokenCode(value1);
        dto.setTokenloseDate("1");

        return Result.ok();
    }

    @ApiOperation("同步区域数据")
    @GetMapping("/sync")
    public Result syncAreaData(){
        String result="{\"code\":\"0\",\"data\":[{\"date_time\":\"2021-08-10 11:20:10\",\"cnt_resi\":12450,\"cnt\":89875}],\"description\":\"\",\"success\":true}";
        ApiResult<List<AreaPersonDto>> apiResult = JSON.parseObject(result, new TypeReference<ApiResult<List<AreaPersonDto>>>(){});
        List<AreaPersonDto> data = apiResult.getData();
        Date date = new Date();
        List<AreaPerson> areaPeople = BeanTransUtil.convertLists(data, AreaPerson.class, (t, o) -> {
            t.setAreaId(2);
            t.setCreateTime(date);
        });
        areaPersonService.saveBatch(areaPeople);
        return Result.ok();
    }


    @GetMapping("/openplat-sys-core/api/interface/sys/channelinterface/createtoken")
    public String createtoken() {
        return "{\"code\":\"10000\",\"result\":\"成功\",\"date\":\"2017-11-30 17:31:01\",\"data\":{\"tokenCode\":\"EET00YCESOWT2XR34S3XC7AVOXE7HSJL5976K67D06L4XYF859\",\"tokenloseDate\":\"2017-11-30 18:31:01\"}}";
    }
}

